package quartz;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

public class QuartzMain {

    public static void main(String[] args) throws SchedulerException {

        //  1. 任务调度器
        Scheduler scheduler = QuartzMain.createScheduler();
        // 2. 创建一个任务
        JobDetail job = QuartzMain.createJob();
        // 3. 创建任务的时间触发器
        Trigger trigger = QuartzMain.createTrigger();
        // 4. 使用任务调度其根据时间触发器执行任务
        scheduler.scheduleJob(job,trigger);
        scheduler.start();
    }

    public static Scheduler createScheduler() throws SchedulerException {
        SchedulerFactory schedulerFactory = new StdSchedulerFactory();
        Scheduler scheduler = schedulerFactory.getScheduler();
        return scheduler;
    }

    public static JobDetail createJob(){
        JobBuilder jobBuilder = JobBuilder.newJob(MyJob.class);    //TODO： 自定义任务类.
        jobBuilder.withIdentity("myJobTest");
        JobDetail jobDetail = jobBuilder.build();
        return jobDetail;
    }

    public static Trigger createTrigger(){
        CronTrigger cronTrigger = TriggerBuilder.newTrigger()
                .withIdentity("triggerName","myTrigger")
                .startNow()
                .withSchedule(CronScheduleBuilder.cronSchedule("*/2 * * * * ?")).build();
        return cronTrigger;
    }
}
